package org.jeecg.modules.tls.blog.utils;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import org.apache.commons.io.FilenameUtils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @description:oss图片上传测试类
 * @author: 天之璎珞
 */

@Component
public class UploadUtils {
    //读取配置文件内容
    @Value("${aliyun.oss.file.endpoint}")
    public static final String ALI_DOMAIN = "https://pai-xue-yuan.oss-cn-shenzhen.aliyuncs.com/";

    public static String uploadImage(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String ext ="." + FilenameUtils.getExtension(originalFilename);
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String filename ="blog/" + uuid + ext;
        String endpoint = "http://oss-cn-shenzhen.aliyuncs.com";
        String accessKeyId = "LTAI5tKN17aTWzAFfYPVqB9x";
        String accessSecret = "bhLJUvDlsNS0qSKQATOP5KjFK3uSHE";
        OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyId,accessSecret);
        ossClient.putObject("pai-xue-yuan",filename,file.getInputStream());
        ossClient.shutdown();
        return ALI_DOMAIN + filename;
    }
}
